#include "../include/head.h"
#include "../include/func.h"


int main(int argc, char *argv[])
{
    //命令行参数：配置文件路径
    ARGS_CHECK(argc, 2); 

    //拿到服务器ip和port
    char ip_server[16] = "";
    int port_server;
    int ret = conf_get(argv[1], ip_server, &port_server);
    ERROR_CHECK(ret, -1, "conf_get");
    
    //连接服务器
    socket_t fd_server = tcp_connect(ip_server, port_server);
    ERROR_CHECK(fd_server,-1,"socket_server");
    
    //从stdin中读取命令，发给服务器
    char line[128] = "";
    cmd_t cmd;
    train_t train;//大火车，传输数据
    while (1) {
        memset(&train, 0, sizeof(train));
        memset(&cmd, 0, sizeof(cmd_t));
        memset(line, 0, sizeof(line));
    
        //从标准输入中读取命令
        read(STDIN_FILENO, line, sizeof(line));
        if (strlen(line) > 0) {
    line[strlen(line) - 1] = '\0';

}
        if (!strcmp(line, "quit")) {
            exit(1); 
        }


        //格式化这行数据，并存入cmd
        int ret = init_cmd(line, &cmd);
        ERROR_CHECK(ret,-1,"init_cmd");
    
        //判断命令是否合法
        if (-1 == cmd_check(&cmd)) {
            printf("the cmd is illegal!\n");
            continue;
        }

//
        ret = print_cmd(&cmd);
        ERROR_CHECK(ret, -1, "print_cmd");

        train.tag = command_transfer(cmd.cmd[0]);
        printf("Command: %s -> Tag: %d\n", cmd.cmd[0], train.tag);
    
        //将cmd打包放入小货车
        memcpy(train.data, &cmd, sizeof(cmd));
    
        //将命令发送给服务器
        train.data_length = sizeof(cmd_t);
        send(fd_server, &train.tag, sizeof(int), 0);
        send(fd_server, &train.data_length, sizeof(int), 0);
        send(fd_server, train.data, train.data_length, 0);
    
        //等待服务器返回数据
        if (train.tag == TAGCD) {
            system("clear");
            //先接长度，后接数据
            recv(fd_server, &train.tag, sizeof(int), 0);
            recv(fd_server, &train.data_length, sizeof(int), 0);
            recv(fd_server, train.data, train.data_length, MSG_WAITALL);
            printf("%s\n", train.data);
        }
        else if (train.tag == TAGPWD) {
            system("clear");
            //先接长度，后接数据
            recv(fd_server, &train.tag, sizeof(int), 0);
            recv(fd_server, &train.data_length, sizeof(int), 0);
            recv(fd_server, train.data, train.data_length, MSG_WAITALL);
            printf("%s\n", train.data);
        }
        else if (train.tag == TAGREMOVE) {
            system("clear");
            //先接长度，后接数据
            recv(fd_server, &train.tag, sizeof(int), 0);
            recv(fd_server, &train.data_length, sizeof(int), 0);
            recv(fd_server, train.data, train.data_length, MSG_WAITALL);
            printf("%s\n", train.data);
        }
        else if (train.tag == TAGMKDIR) {
            system("clear");
            //先接长度，后接数据
            recv(fd_server, &train.tag, sizeof(int), 0);
            recv(fd_server, &train.data_length, sizeof(int), 0);
            recv(fd_server, train.data, train.data_length, MSG_WAITALL);
            printf("%s\n", train.data);
        }
        else if (train.tag == TAGLS) {
            system("clear");
            //先接长度，后接数据
            recv(fd_server, &train.tag, sizeof(int), 0);
            while (1) {
                recv(fd_server, &train.data_length, sizeof(int), 0);
                /* printf("data_len: %d\n", train._data_len); */
                if (0 == train.data_length) {
                    break;
                }
                recv(fd_server, train.data, train.data_length, MSG_WAITALL);
                printf("%s\n", train.data);
            }
        }
        else if (train.tag == TAGPUTS) {
            system("clear");
            //上传文件
            upload(fd_server, cmd.cmd[1]);
            //先接长度，后接数据
            recv(fd_server, &train.tag, sizeof(int), 0);
            recv(fd_server, &train.data_length, sizeof(int), 0);
            recv(fd_server, train.data, train.data_length, MSG_WAITALL);
            printf("%s\n", train.data);
        }
        else if (train.tag == TAGGETS) {
            system("clear");
            //下载文件
            download(fd_server, cmd.cmd[1]);
            //先接长度，后接数据
            recv(fd_server, &train.tag, sizeof(int), 0);
            recv(fd_server, &train.data_length, sizeof(int), 0);
            recv(fd_server, train.data, train.data_length, MSG_WAITALL);
            printf("%s\n", train.data);
        }
    }
    return 0;

}
